{layout name="layout" /}

<nav class="breadcrumb">
    <i class="Hui-iconfont">&#xe67f;</i>
    <a href="{:url('admin/index/index')}"> 首页</a>
    <span class="c-gray en">&gt;</span>
    <a href="{:url('admin/topic_content/index')}">专题管理</a>
    <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
        href="javascript:location.replace(location.href);" title="刷新">
        <i class="Hui-iconfont">&#xe68f;</i>
    </a>
</nav>

<div class="Hui-article">
    <article class="cl pd-20" style="min-width: 900px;">
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <a href="javascript:;" onclick="del_all()" class="btn btn-danger radius">
                    <i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
                <a class="btn btn-primary radius" href="javascript:void(0);" onclick="addTopic()">
                    <i class="Hui-iconfont">&#xe600;</i> 添加专题</a>
            </span>
            <span class="select-box inline ml-50">
                <select name="cid" id="sel_option" class="select">
                    <option value="0" {eq name='cid' value="0" }selected{/eq}>所有栏目</option>
                    {foreach $categoryTree as $value}
                    <option value="{$value.id}" {eq name='cid' value="$value.id" }selected {/eq} {neq name='value.type'
                        value='0' }disabled style="background-color: rgb(223, 220, 217);" {/neq}>{$value.name}</option>
                    {notempty name="value.child"}
                    {foreach $value.child as $val}
                    <option value="{$val.id}" {eq name='cid' value="$val.id" }selected {/eq} {neq name='val.type'
                        value='0' }disabled style="background-color: rgb(223, 220, 217);" {/neq}>--{$val.name}</option>
                    {notempty name="val.child"}
                    {foreach $val.child as $vo}
                    <option value="{$vo.id}" {eq name='cid' value="$vo.id" }selected {/eq} {neq name='vo.type' value='0'
                        }disabled style="background-color: rgb(223, 220, 217);" {/neq}>&nbsp;&nbsp;└ --{$vo.name}
                    </option>
                    {/foreach}
                    {/notempty}
                    {/foreach}
                    {/notempty}
                    {/foreach}
                </select>
            </span>
            <span class="r">共有数据：<strong id="total">{notempty name="data"}{$data->total()}{else/}0{/notempty}</strong>
                条</span>
        </div>
        <div id="edatalist">
            <table class="table table-border table-bordered table-bg">
                <thead>
                    <tr class="text-c">
                        <th width="25px;">
                            <input type="checkbox" name="" value="">
                        </th>
                        <th width="80px;">ID</th>
                        <th style="min-width: 260px;">标题</th>
                        <th width="200px;">栏目</th>
                        <th width="120px;">发布者</th>
                        <th width="160px;">发布时间</th>
                        <th width="80px;">状态</th>
                        <th width="80px;">推荐</th>
                        <th width="80px;">排序</th>
                        <th width="80px;">点击量</th>
                        <th width="180px;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach $data as $val}
                    <tr class="text-c va-m">
                        <td>
                            <input type="checkbox" value="{$val.id}" name="checkbox[]">
                        </td>
                        <td>{$val.id}</td>
                        <td class="text-l">
                            <a href="{$val.topic_url ?: '/topic/'.$val['id'].'.html'}" title="浏览"
                                target="_blank">{$val.title}</a>
                        </td>
                        <td>{$categories[$val.cid]}</td>
                        <td>{$val.username}</td>
                        <td>{$val.create_time|date="Y-m-d H:i:s"}</td>
                        <td class="td-status">
                            <a href="javascript:void(0);" onclick="status('{$val.id}', this)"
                                onclick="status('{$val.id}', this)" {eq name="val.status" value="2" } title="上线"><span
                                    class="label label-danger">
                                    {else/}
                                    title="下架"><span class="label label-default">
                                        {/eq}
                                        {$status[$val.status]} </span></a>
                        </td>
                        <td class="td-status"><a href="javascript:void(0);" onclick="dotop('{$val.id}', this)" {eq
                                name="val.top" value="2" } title="取消推荐"><span class="label label-success">
                                    {else/}
                                    title="推荐"><span class="label label-default">
                                        {/eq}
                                        {$top[$val.top]} </span></a></td>
                        <td>{$val.sort}</td>
                        <td>{$val.clicks}</td>
                        <td class="td-manage">
                            <a href="{:url('topic_content/info', ['cid'=>$val.cid,'id'=>$val.id])}" title="修改">修改</a>
                            <a href="javascript:void(0);" title="删除" class="ml-10"
                                onClick="del(this,'{$val.id}')">删除</a>
                            <a href="{:url('topic_sun/index', ['id'=>$val.id])}" title="更新子专题" class="ml-10">子专题</a>
                            <a href="{:url('topic_info/index', ['id'=>$val.id])}" title="专题信息管理" class="ml-10">专题信息</a>
                        </td>
                    </tr>
                    {/foreach}
                </tbody>
            </table>
        </div>
        <div class="cl pd-5 bg-1 bk-gray mt-20 ">
            <span class="r">{notempty name="data"}{$data->render()|raw}{/notempty}</span>
        </div>
    </article>
</div>
<script>
    $("#sel_option").change(function () {
        var cid = $("#sel_option").val();
        // console.log(cid);
        if (cid) {
            window.location.href = '/admin/topic_content/index/cid/' + cid + '.html';
        } else {
            window.location.href = '/admin/topic_content/index.html';
        }
        return false;
    })

    function addTopic() {
        var cid = $("#sel_option").val();
        // console.log(cid);
        // return false;
        if (!cid || cid == 0) {
            layer.msg("请选择栏目", {
                icon: 5,
                time: 2000
            });
            return false;
        }
        window.location.href = '/admin/topic_content/info/cid/' + cid + '.html';
    }

    function status(id, obj) {
        $.post('/admin/topic_content/status', {
            'id': id
        }, function (data) {
            console.log(data);
            if (data.code == 1) {
                // console.log($(obj));
                $(obj).children('span').removeClass();
                if (data.status == 2) {
                    $(obj).attr("title", "下线");
                    $(obj).children('span').addClass("label label-default");
                    $(obj).children('span').html("正常");
                } else {
                    $(obj).attr("title", "上线");
                    $(obj).children('span').addClass("label label-danger");
                    $(obj).children('span').html("下架");
                }
                layer.msg(data.msg, {
                    icon: 1,
                    time: 2000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    }

    function dotop(id, obj) {
        $.post('/admin/topic_content/dotop', {
            'id': id
        }, function (data) {
            console.log(data);
            if (data.code == 1) {
                // console.log(obj);
                $(obj).children('span').removeClass();
                if (data.top == 2) {
                    $(obj).attr("title", "取消推荐");
                    $(obj).children('span').addClass("label label-success");
                    $(obj).children('span').html("推荐");
                } else {
                    $(obj).attr("title", "推荐");
                    $(obj).children('span').addClass("label label-default");
                    $(obj).children('span').html("非推荐");
                }

                layer.msg(data.msg, {
                    icon: 1,
                    time: 2000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    }

    function del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.post('/admin/topic_content/delete', {
                'id': id
            }, function (data) {
                if (data.code == 1) {
                    console.log($(obj));
                    $(obj).parents("tr").remove();
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    });
                } else {
                    layer.msg(data.msg, {
                        icon: 5,
                        time: 2000
                    });
                    return false;
                }
            }, 'json');
        });
    }
</script>